package nancal.mp.service

import io.fabric8.istio.api.networking.v1beta1.VirtualService
import io.fabric8.istio.client.DefaultIstioClient
import io.fabric8.istio.client.IstioClient
import io.fabric8.kubernetes.client.ConfigBuilder
import nancal.mp.db.mongo.extend.getAppDeploySetting
import nancal.mp.db.mongo.mor
import org.springframework.stereotype.Service


@Service
class IstioService {
    companion object {
        fun getIstioClient(env: String): IstioClient {
            var deploySetting = mor.dev.appDeploySetting.getAppDeploySetting(env)

            return DefaultIstioClient(
                ConfigBuilder()
                    .withMasterUrl(deploySetting.k8sConfig.apiServerUrl)
                    .withTrustCerts()
                    .withOauthToken(deploySetting.k8sConfig.apiToken)
                    .build()
            )
        }
    }


    fun findVss(env: String, namespace: String): MutableList<VirtualService> {
        var client = getIstioClient(env);
        return client.v1beta1().virtualServices()
            .inNamespace(namespace)
            .list()
            .items
    }

    fun applyVirtualServiceYaml(targetEnv: String, namespace: String, dockerName: String, devUserIfIstio: String) {


    }

    fun applyDestinationRuleYaml(targetEnv: String, namespace: String, dockerName: String) {

    }
}